<?php
require_once ("../src/db/connectdatabase.php");

function getNew($idnew) {
	$con = new ConnectDB();
	$tbl = "news";
	$fn = array('new_id', 'new_title', 'new_content', 'new_tomtat', 'idtnew');
	$condetion = array('new_id' => $idnew);
	return $con -> getvalue($tbl, $fn, $condetion);
}

if(isset($_GET['id'])) {
	$idnew = $_GET['id'];
	$new = getNew($idnew);
	
	if(count($new) == 1) {
?>
<script src="../ckeditor/ckeditor.js"></script>
<style>
textarea {
	margin:5px;
	padding:5px;
}
</style>
<h3>Cập Nhật Tin Tức</h3>
<div style="color: red;">
<?php
	if(isset($_GET['mess'])) {
		echo($_GET['mess']);
	}
?>
</div>
<div style="width: 850px;">
	<form action="../src/control/adminnew.php" method="post" onSubmit="return checkvaluequestion();">
        <table>
        	<tr>
                <td valign="top">Loại Tin Tức: <select name="form_typenew" id="form_statussearch" style="height: 23px; width:200px; margin-right:10px;"><option value="">--Chọn--</option></select></td>
             </tr>
        	<tr>
                <td valign="top">Tiêu Đề</td>
             </tr>
            <tr>
                <td>
                <input type="hidden" value="<?php echo($new[0][0]) ?>" name="idedit" />
                	<textarea style="margin:5px; padding:5px;width:750px;" cols="80" id="tieude" name="tieude" rows="3"><?php echo($new[0][1]) ?></textarea>
                </td>
            </tr>
            <tr>
                <td valign="top">Nội Dung Tóm Tắt</td>
            </tr>
            <tr>
                <td>
                	<textarea style="margin:5px; padding:5px; width:750px;" cols="80" id="tomtat" name="tomtat" rows="10"><?php echo($new[0][3]) ?></textarea>
                </td>
            </tr>
            <tr>
            	<td valign="top">Nội Dung</td>
            </tr>
            <tr>
                <td>
                	<textarea id="textfieldcontent" name="textfieldcontent" rows="40" cols="200" style="width: 750px; max-width:750px; min-width:750px;" class="tinymce"><?php echo($new[0][2]) ?></textarea>
                </td>
            </tr>
            <tr>
            	<th><input type="hidden" name="acc" value="edit" /><input type="submit" value="Cập Nhật" style="border-radius: 5px; padding:5px; width:100px; background:#03F; color:#FFF; font-weight:bold;" /></th>
            </tr>
        </table>
    </form>
</div>
<script>
CKEDITOR.replace('tomtat');
function checkvaluequestion() {
	var tieude = $.trim($('#tieude').val());
	var cauhoi = CKEDITOR.instances['tomtat'].getData();
	var answer = $.trim($('#textfieldcontent').val());
	
	if(tieude.length == 0) {
		alert("Vui lòng nhập thông tin tiêu đề!");
		$('#tieude').focus();
		return false;
	}
	
	if(cauhoi.length == 0) {
		alert("Vui lòng nhập thông tin tóm tắt!");
		$('#tomtat').focus();
		return false;
	}
	
	if(answer.length == 0) {
		alert("Vui lòng nhập thông tin nội dung!");
		$('#textfieldcontent').focus();
		return false;
	}
}

loadtypenews();
function loadtypenews() {
	$.ajax({
		url: "../src/ajax/ajaxAdminTypeNew.php",
		type: "post",
		data: {act:'getall'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + '</option>';
			});
			$('#form_statussearch').append(html);
			$('#form_statussearch').val("<?php echo($new[0][4]) ?>");
		}
	});
}
</script>
<?php
	}
}
?>